﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Ticket
{
    public partial class frmFunds : frmBase
    {
        #region Contructor
        public frmFunds()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.Funds boFunds = new Business.Funds();
        string sModel = ""; //Store AND or UPDATE status
        private const string ADD_MODEL = "ADD";
        private const string UPDATE_MODEL = "UPDATE";
        #endregion

        #region Event
        private void frmFunds_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật vốn";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_20");
            
            //bind data to combobox type
            ChooseType();

            //set status
            LoadStatusOnce();
            LoadStatus(true, false, false, false, false, true);

            View();
        }

        private void lvw_SelectedIndexChanged(object sender, EventArgs e)
        {
            sModel = "";
            IsEdit();
            if (lvw.SelectedItems.Count > 0)
            {
                ListViewItem item = lvw.SelectedItems[0];
                string[] arr = item.Tag.ToString().Split(';');
                string type = "";
                string unit = "";
                try
                {
                    type = arr[0];
                    unit = arr[1];
                }
                catch {}

                DataObjects.Funds oFunds = boFunds.GetObjectByPK(type, unit);
                if (oFunds != null)
                {
                    if (oFunds.Type == "CUR_1" || oFunds.Type == "CUR_2")
                    {
                        radCash.Checked = true;
                        cboType.SelectedValue = oFunds.Type;
                    }
                    else
                    {
                        radBank.Checked = true;
                        cboType.SelectedValue = oFunds.Type;
                    }
                    txtUnit.Text = oFunds.Unit;
                    txtMoney.Text = objMethod.FormatCurency(oFunds.Money.ToString());
                    txtRemark.Text = oFunds.Remark;
                    LoadStatus(true, true, true, false, false, true);
                }
                else
                    LoadStatus(true, false, false, false, false, true);
                
            }
            else
            {
                LoadStatus(true, false, false, false, false, true);
            }
        }

        private void radCash_CheckedChanged(object sender, EventArgs e)
        {
            ChooseType();
        }

        private void radBank_CheckedChanged(object sender, EventArgs e)
        {
            ChooseType();
        }

        private void txtMoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            objMethod.InputNumber(e, txtMoney.Text);
        }

        private void txtMoney_Leave(object sender, EventArgs e)
        {
            txtMoney.Text = objMethod.FormatCurency(txtMoney.Text);
        }
        #endregion

        #region Execute
        public override void Add()
        {
            if (cboType.SelectedValue.ToString().Contains("CUR"))
            {
                txtUnit.Text = cboType.SelectedValue.ToString();
                txtUnit.Enabled = false;
            }
            else
            {
                txtUnit.Text = "";
                txtUnit.Enabled = true;
            }
            txtMoney.Text = "";
            dtpDateInput.Value = DateTime.Now;
            txtRemark.Text = "";

            LoadStatus(false, false, false, true, true, false);
            sModel = ADD_MODEL;
            IsEdit();
            cboType.Focus();
        }

        public override void Edit()
        {
            if (txtUnit.Text == "")
                return;
            LoadStatus(false, false, false, true, true, false);
            sModel = UPDATE_MODEL;
            IsEdit();
            cboType.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (txtUnit.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //} 

                    //delete
                    DataObjects.Funds oFunds = boFunds.GetObjectByPK(cboType.SelectedValue.ToString(), txtUnit.Text);
                    if (oFunds != null)
                    {
                        boFunds.Delete(oFunds);
                        MessageBox.Show("Xóa thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        View();
                    }
                    else
                        MessageBox.Show("Không thể xóa được.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Cancel()
        {
            if (cboType.SelectedValue.ToString().Contains("CUR"))
            {
                txtUnit.Text = cboType.SelectedValue.ToString();
                txtUnit.Enabled = false;
            }
            else
            {
                txtUnit.Text = "";
                txtUnit.Enabled = true;
            }
            txtMoney.Text = "";
            dtpDateInput.Value = DateTime.Now;
            txtRemark.Text = "";
            LoadStatus(false, false, false, false, false, true);
            View();
        }

        public override void Save()
        {
            if (CheckValid())
            {
                try
                {
                    if (sModel.Trim().ToUpper().Equals(ADD_MODEL))
                    {
                        if (CheckIsExist(cboType.SelectedValue.ToString(), txtUnit.Text))
                        {
                            MessageBox.Show("Vốn đã tồn tại.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            cboType.Focus();
                            return;
                        }

                        AddFunds();
                    }
                    else if (sModel.Trim().ToUpper().Equals(UPDATE_MODEL))
                    {
                        UpdateFunds();
                    }
                    MessageBox.Show("Cập nhật vốn thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    View();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật vốn.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void View()
        {
            try
            {
                lvw.Items.Clear();

                Business.CodeType boCodeType = new Business.CodeType();
                DataObjects.CodeType oCodeType;

                ArrayList arrFunds = new ArrayList();
                arrFunds = boFunds.List();

                ListViewItem item;

                foreach (DataObjects.Funds oFunds in arrFunds)
                {
                    item = new ListViewItem();
                    item.Tag = oFunds.Type + ";" + oFunds.Unit;
                    oCodeType = boCodeType.GetObjectByPK(oFunds.Type);
                    if (oCodeType != null)
                        item.Text = oCodeType.CodeTypeName;
                    else
                        item.Text = "";
                    item.SubItems.Add(oFunds.Unit);
                    item.SubItems.Add(objMethod.FormatCurency(oFunds.Money.ToString()));
                    item.SubItems.Add(oFunds.DateInput.Value.ToString("dd/MM/yyyy"));
                    item.SubItems.Add(oFunds.Remark);

                    lvw.Items.Add(item);
                }

                if (lvw.Items.Count > 0)
                {
                    lvw.Items[0].Selected = true;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Method
        private void LoadStatusOnce()
        {
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnFind.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blEdit, bool blDelete, bool blCancel, bool blSave, bool blView)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnEdit.Enabled = blEdit & bEdit;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnCancel.Enabled = blCancel & bCancel;
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnView.Enabled = blView & bView;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnEdit.ButtonColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnEdit.ForeColor = exeCtrl.btnEdit.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = exeCtrl.btnCancel.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = exeCtrl.btnView.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void IsEdit()
        {
            if (sModel == ADD_MODEL | sModel == UPDATE_MODEL)
            {
                radBank.Enabled = true;
                radCash.Enabled = true;
                cboType.Enabled = true;
                txtUnit.Enabled = true;
                txtMoney.Enabled = true;
                dtpDateInput.Enabled = true;
                txtRemark.Enabled = true;
            }
            else
            {
                radBank.Enabled = false;
                radCash.Enabled = false;
                cboType.Enabled = false;
                txtUnit.Enabled = false;
                txtMoney.Enabled = false;
                dtpDateInput.Enabled = false;
                txtRemark.Enabled = false;
            }
        }

        private bool CheckValid()
        {
            if (cboType.DataSource == null || cboType.SelectedValue == null)
            {
                MessageBox.Show("Vui lòng chọn Loại/Ngân hàng.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cboType.Focus();
                return false;
            }

            if (txtUnit.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập số tài khoản.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUnit.Focus();
                return false;
            }

            if (txtMoney.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập số tiền.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtMoney.Focus();
                return false;
            }

            return true;
        }

        private bool CheckIsExist(string type, string unit)
        {
            DataObjects.Funds oFunds = boFunds.GetObjectByPK(type, unit);
            if (oFunds != null)
            {
                return true;
            }
            return false;
        }

        private void AddFunds()
        {
            //Add new 
            DataObjects.Funds oFunds = new DataObjects.Funds();
            oFunds.Type = cboType.SelectedValue.ToString();
            oFunds.Unit = txtUnit.Text;
            oFunds.Money = double.Parse(txtMoney.Text);
            oFunds.DateInput = dtpDateInput.Value.Date;
            oFunds.Remark = txtRemark.Text;

            //Insert
            boFunds.Insert(oFunds);
        }

        private void UpdateFunds()
        {
            //update
            DataObjects.Funds oFunds = new DataObjects.Funds();
            oFunds.Type = cboType.SelectedValue.ToString();
            oFunds.Unit = txtUnit.Text;
            oFunds.Money = double.Parse(txtMoney.Text);
            oFunds.DateInput = dtpDateInput.Value.Date;
            oFunds.Remark = txtRemark.Text;

            boFunds.Update(oFunds);
        }

        private void ChooseType()
        {
            if (radCash.Checked)
            {
                clsCommonFunc.GetListCodeType(cboType, "CUR");
                txtUnit.Text = cboType.SelectedValue.ToString();
                txtUnit.Enabled = false;
            }
            else
            {
                clsCommonFunc.GetListCodeType(cboType, "BANK");
                //txtUnit.Text = "";
                txtUnit.Enabled = true;
            }
        }
        #endregion

    }
}
